package com.ybt.communicate.ws.parser;

import java.io.FileInputStream;
import java.util.Iterator;

import org.dom4j.Element;

import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.communicate.core.parser.IProtocolParser;
import com.ybt.communicate.core.parser.impl.CommonProtocolParser;
import com.ybt.communicate.ws.configration.WSBaseConfig;
import com.ybt.communicate.ws.configration.WSConstants;
import com.ybt.communicate.ws.configration.WSProtocolConfig;

public class WSParser implements IProtocolParser<WSProtocolConfig> {

    public void convertCommon(WSProtocolConfig protocolConfig,
            Element protocol) throws ProtocolException {
        // TODO Auto-generated method stub
        WSBaseConfig config = protocolConfig.getCommonConfig();
        Element common = protocol.addElement(WSConstants.TAG_ELE_COMMON);
        if (config.getTarget() != null) {
            //wsdl地址
            common.addAttribute(WSConstants.TAG_ATTR_TARGET, config.getTarget());
        }
        if (config.getMode() != null) {
            //rpc or document
            common.addAttribute(WSConstants.TAG_ATTR_MODE, config.getMode());
        }
    }

    public void convertRequest(WSProtocolConfig protocolConfig,
            Element protocol) throws ProtocolException {
        // TODO Auto-generated method stub
        WSBaseConfig config = protocolConfig.getReqConfig();
        Element request = protocol.addElement(WSConstants.TAG_ELE_REQUEST);
        if (config.getMethod() != null) {
            //方法名
            request.addAttribute(WSConstants.TAG_ATTR_METHOD, config.getMethod());
        }
        if (config.getTargetNamespace() != null) {
            //配置了名称空间
            request.addAttribute(WSConstants.TAG_ATTR_TARGET_NAMESPACE, config.getTargetNamespace());
        }
        //解析参数
        for(String p : config.getParameters()){
            Element pe = request.addElement(WSConstants.TAG_ELE_PARAMETER);
            pe.addAttribute(WSConstants.TAG_ATTR_VALUE, p);
            
        }
    }

    public void convertResponse(WSProtocolConfig protocolConfig,
            Element protocol) throws ProtocolException {
        // TODO Auto-generated method stub
        WSBaseConfig config = protocolConfig.getRespConfig();
        Element response = protocol.addElement(WSConstants.TAG_ELE_RESPONSE);
        if (config.getMethod() != null) {
            //方法名
            response.addAttribute(WSConstants.TAG_ATTR_METHOD, config.getMethod());
        }
        if (config.getTargetNamespace() != null) {
            //配置了名称空间
            response.addAttribute(WSConstants.TAG_ATTR_TARGET_NAMESPACE, config.getTargetNamespace());
        }
        //解析参数
        for(String p : config.getParameters()){
            Element pe = response.addElement(WSConstants.TAG_ELE_PARAMETER);
            pe.addAttribute(WSConstants.TAG_ATTR_VALUE, p);
            
        }
    }

    public WSProtocolConfig parseCommon(WSProtocolConfig config,
            Element common) throws ProtocolException {
        if (config.getCommonConfig() == null) {
            config.setCommonConfig(new WSBaseConfig());
        }
        WSBaseConfig commonConfig = config.getCommonConfig();

        // target
        String target = common.attributeValue(WSConstants.TAG_ATTR_TARGET);
        if (target != null) {
            commonConfig.setTarget(target);
        }
        
        // ws 模式
        String mode = common.attributeValue(WSConstants.TAG_ATTR_MODE);
        if (mode != null) {
            commonConfig.setMode(mode);
        }

        return config;
    }

    public WSProtocolConfig parseRequest(WSProtocolConfig config,
            Element request) throws ProtocolException {
        // TODO Auto-generated method stub
        if (config.getReqConfig() == null) {
            config.setReqConfig(new WSBaseConfig());
        }
        WSBaseConfig reqConfig = config.getReqConfig();

        // ws 方法
        String method = request.attributeValue(WSConstants.TAG_ATTR_METHOD);
        if (method != null) {
            reqConfig.setMethod(method);
        }

        // 名称空间
        String namespace = request.attributeValue(WSConstants.TAG_ATTR_TARGET_NAMESPACE);
        if (namespace != null) {
            reqConfig.setTargetNamespace(namespace);
        }

        //解析参数
        Iterator<Element> pit = request.elementIterator(WSConstants.TAG_ELE_PARAMETER);
        while(pit.hasNext()){
            Element pe = pit.next();
            reqConfig.addParameter(pe.attributeValue(WSConstants.TAG_ATTR_VALUE));
        }
        return null;
    }

    public WSProtocolConfig parseResponse(WSProtocolConfig config,
            Element response) throws ProtocolException {
        // TODO Auto-generated method stub
        if (config.getRespConfig() == null) {
            config.setRespConfig(new WSBaseConfig());
        }
        WSBaseConfig respConfig = config.getRespConfig();

        // ws 方法
        String method = response.attributeValue(WSConstants.TAG_ATTR_METHOD);
        if (method != null) {
            respConfig.setMethod(method);
        }

        // 名称空间
        String namespace = response.attributeValue(WSConstants.TAG_ATTR_TARGET_NAMESPACE);
        if (namespace != null) {
            respConfig.setTargetNamespace(namespace);
        }

        //解析参数
        Iterator<Element> pit = response.elementIterator(WSConstants.TAG_ELE_PARAMETER);
        while(pit.hasNext()){
            Element pe = pit.next();
            respConfig.addParameter(pe.attributeValue(WSConstants.TAG_ATTR_VALUE));
        }
        return null;
    }

    public static void main(String[] args) {
        String f = "d:\\eclipse\\sgworkspance\\YBT_Simple\\conf\\protocols\\WSClient_newCont.xml";
        CommonProtocolParser<WSProtocolConfig> parser = new CommonProtocolParser<WSProtocolConfig>();
        try {
            WSProtocolConfig config = parser.parse(new FileInputStream(f));
            System.out.println(parser.convert(config));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
